import axios from "axios";
import { isToken } from "./token";

const instance = axios.create({
  Timeout: 30000,
  // baseURL: "http://192.168.36.185:1337",
  // baseURL: "47.104.185.33:8002",
  // baseURL: "http://192.168.15.59:1337",
  // baseURL: "http://localhost:1337",
});

// 全局拦截器
instance.interceptors.request.use(
  function (config) {
    console.log(config);
    config.headers.Authorization = isToken();
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);
// 全局响应拦截，网络请求完成之后执行
instance.interceptors.response.use(
  function (response) {
    return response.data;
  },
  function (error) {
    return Promise.reject(error);
  }
);

/**
 * get请求
 */
export const get = (url, params) => instance.get(url, { params });

/**
 * post请求
 */
export const post = (url, data) => instance.post(url, data);

/**
 * delete请求
 */
export const del = (url) => instance.delete(url);

/**
 * put请求
 */
export const put = (url, data) => instance.put(url, data);
